#include <string.h>
#include <iostream>

using std::cout;
using std::endl;

class Student
{
public:
    Student(const char * name, int id)
    : _name(new char[strlen(name) + 1]())
    , _id(id)
    {
        cout << "Student(const char*, int)" << endl;
        strcpy(_name, name);
    }

    void print() const
    {
        cout << "name:" << _name << endl;
        cout << "id:" << _id << endl;
    }


    ~Student()
    {
        cout << "~Student()" << endl;
        if(_name) {
            delete [] _name;
            _name = nullptr;
        }
    }

private:
    //只能生成栈对象，不能生成堆对象
    void * operator new(size_t sz);
    //重载标准库函数operator delete
    void operator delete(void * ret);


private:
    char * _name;
    int _id;
};

int main()
{
    //栈对象创建时, 调用构造函数
    //栈对象销毁时，也会自动调用析构函数
    Student stu("Jackie", 100);
    stu.print();

    //Student * pstu = new Student("Rose", 101);//error

    return 0;
}

